package com.cmc.cloud.cmclink.doc.util;

import com.cmc.cloud.cmclink.doc.entity.BlnoDO;
import com.cmc.cloud.cmclink.doc.service.si.BlnoService;
import com.cmc.cloud.cmclink.doc.vo.blnochargevo.BlnoLocalChargeResultVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 费用计算util 
 * @author dev89 
 * @date 2025/09/18 09:45
**/
@Component
@Slf4j
public class CostChargeWithBlnoUtil {
    @Resource
    private BlnoService blnoService;

    /**
     * 将提单返回map转成list
     *
     * @param ids 提单ids
     * @param errMsgMap 提单返回信息map
     * @return java.util.List<com.cmc.cloud.cmclink.doc.vo.blnochargevo.BlnoLocalChargeResultVO>
     */
    public List<BlnoLocalChargeResultVO> transitMapToList(Collection<Long> ids, Map<Long, String> errMsgMap,
        String costType) {
        if (CollectionUtils.isEmpty(ids)) {
            return new ArrayList<>();
        }
        List<BlnoDO> blnoList = blnoService.getBlnoList(ids);
        Map<Long, String> blnoMap =
            blnoList.stream().collect(Collectors.toMap(BlnoDO::getId, BlnoDO::getBlNo, (exist, replacement) -> exist));

        return errMsgMap.entrySet().stream().map(entry -> {
            BlnoLocalChargeResultVO rsp = new BlnoLocalChargeResultVO();
            Long id = entry.getKey();
            String blNo = blnoMap.getOrDefault(id, String.valueOf(id));
            rsp.setBlNo(blNo);
            rsp.setMsg(entry.getValue());
            rsp.setType(costType);
            return rsp;
        }).collect(Collectors.toList());
    }
}
